import * as actionTypes from './constants'
import {
  getArticleList,
  getArticleDetail,
  getArticleByClassify
} from '@/network/blog/article.request'
import { getUserProfile, getUserOpus } from '@/network/blog/user.request'
import {
  getFriendList,
  getMineFollowList,
  getFollowMineList
} from '@/network/blog/friend.request'
import { getWalls } from '@/network/blog/file.request'
import { getArticleClassify } from '@/network/blog/article.request'
import { getArticleDiscussList } from '@/network/blog/discuss.request'

// 保存用户登录信息
export const changeBlogUserAction = (user) => {
  return (dispatch) => {
    return dispatch({
      type: actionTypes.CHANGE_USER_BASE,
      user: user
    })
  }
}

// 保存文章列表
const changeBlogArticleListAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_LIST,
  articleList: res
})

export const getBlogArticleListAction = (userId, offset, limit) => {
  return (dispatch) => {
    getArticleList(userId, offset, limit)
      .then((res) => {
        dispatch(changeBlogArticleListAction(res))
      })
      .catch((err) => console)
  }
}

// 保存用户详情
const changeUserProfileAction = (res) => ({
  type: actionTypes.CHANGE_USER_PROFILE,
  userProfile: res
})

export const getUserProfileAction = (userId) => {
  return (dispatch) => {
    getUserProfile(userId)
      .then((res) => {
        dispatch(changeUserProfileAction(res))
      })
      .catch((err) => console)
  }
}

// 保存文章详情
const changeArticleDetailAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_DETAIL,
  articleDetail: res
})

export const getArticleDetailAction = (id) => {
  return (dispatch) => {
    getArticleDetail(id)
      .then((res) => {
        dispatch(changeArticleDetailAction(res))
      })
      .catch((err) => console)
  }
}

// 获取用户作品
const changeUserOpusAction = (res) => ({
  type: actionTypes.CHANGE_USER_OPUS,
  userOpus: res
})

export const getUserOpusAction = (userId) => {
  return (dispatch) => {
    getUserOpus(userId)
      .then((res) => {
        dispatch(changeUserOpusAction(res))
      })
      .catch((err) => console)
  }
}

// 获取用户收藏图片
const changeUserWallAction = (res) => ({
  type: actionTypes.CHANGE_USER_WALLS,
  walls: res
})

export const getUserWallAction = (userId) => {
  return (dispatch) => {
    getWalls(userId)
      .then((res) => {
        dispatch(changeUserWallAction(res))
      })
      .catch((err) => console)
  }
}

// 获取文章分类统计
const changeArticleClassifyAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_CLASSIFY,
  classify: res
})

export const getArticleClassifyAction = (userId) => {
  return (dispatch) => {
    getArticleClassify(userId)
      .then((res) => {
        dispatch(changeArticleClassifyAction(res))
      })
      .catch((err) => console)
  }
}

// 根据分类获取文章列表
const changeArticleByClassifyAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_BY_CLASSIFY,
  articleByClassify: res
})

export const getArticleByClassifyAction = (userId, classify) => {
  return (dispatch) => {
    getArticleByClassify(userId, classify)
      .then((res) => {
        dispatch(changeArticleByClassifyAction(res))
      })
      .catch((err) => console)
  }
}

// 获取朋友列表
const changeFriendListAction = (res) => ({
  type: actionTypes.CHANGE_FRIEND_LIST,
  friendList: res
})

export const getFriendListAction = (userId) => {
  return (dispatch) => {
    getFriendList(userId)
      .then((res) => {
        dispatch(changeFriendListAction(res))
      })
      .catch((err) => console)
  }
}

// 保存我关注的用户
const changeMineFollowListAction = (res) => ({
  type: actionTypes.CHANGE_MINE_FOLLOW_LIST,
  mineFollow: res
})

export const getMineFollowListAction = (userId) => {
  return (dispatch) => {
    getMineFollowList(userId).then((res) => {
      dispatch(changeMineFollowListAction(res))
    })
  }
}

// 保存关注我的用户
const changeFollowMineListAction = (res) => ({
  type: actionTypes.CHANGE_FOLLOW_MINE_LIST,
  followMine: res
})

export const getFollowMineListAction = (userId) => {
  return (dispatch) => {
    getFollowMineList(userId).then((res) => {
      dispatch(changeFollowMineListAction(res))
    })
  }
}

// 保存文章评论
const changeArticleDiscussAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_DISCUSS_LIST,
  articleDiscuss: res
})

export const changeDiscussAction = (res) => ({
  type: actionTypes.CHANGE_ARTICLE_DISCUSS_LIST,
  articleDiscuss: res
})

export const getArticleDiscussAction = (articleId, offset = 0, limit = 20) => {
  return (dispatch) => {
    getArticleDiscussList(articleId, offset, limit).then((res) => {
      dispatch(changeArticleDiscussAction(res))
    })
  }
}
